package com.blogs.tools;

import com.blogs.entity.AppConfig;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHandler extends SQLiteOpenHelper {
	protected SQLiteDatabase rdb;
	protected SQLiteDatabase wdb;

	public DBHandler(Context context) {
		super(context, AppConfig.DATA_BASE_NAME, null, 5);
		rdb = getReadableDatabase();
		wdb = getWritableDatabase();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("CREATE TABLE IF NOT EXISTS "
				+ AppConfig.DB_BLOG_LINE_TB
				+ "(blog_id INTEGER,channel nvarchar,author nvarchar,blog_url nvarchar,content nvarchar,title nvarchar,public_time nvarchar,comment varchar,hit varchar,blogapp varchar,PRIMARY KEY (blog_id,channel))");
		db.execSQL("CREATE TABLE IF NOT EXISTS "
				+ AppConfig.DB_NEW_LINE_TB
				+ "(new_id  INTEGER PRIMARY KEY,author nvarchar,new_url nvarchar,content nvarchar,title nvarchar,public_time nvarchar,comment varchar,hit varchar)");
		db.execSQL("CREATE TABLE IF NOT EXISTS " + AppConfig.DB_CONTENT_TB
				+ "(id  INTEGER PRIMARY KEY,content nvarchar,rtime TIMESTAMP default (datetime('now', 'localtime')) )");
		db.execSQL("CREATE TABLE IF NOT EXISTS "
				+ AppConfig.DB_DownLoad_TB
				+ " (id INTEGER primary key autoincrement, downpath varchar(100), threadid INTEGER, downlength INTEGER)");
		db.execSQL("CREATE TABLE IF NOT EXISTS "
				+ AppConfig.DB_FAVORITE_TB
				+ "(id INTEGER PRIMARY KEY AUTOINCREMENT ,blog_id INTEGER,favo_id INTEGER,author nvarchar,blog_url nvarchar,title nvarchar,public_time nvarchar,comment varchar,hit varchar,favo_time nvarchar)");
		db.execSQL("CREATE TABLE IF NOT EXISTS "
				+ AppConfig.DB_COMMENT_QUEUE_TB
				+ "(id INTEGER PRIMARY KEY AUTOINCREMENT ,type INTEGER,comment_id nvarchar,commendBody nvarchar,parentCommentID nvarchar,err_text nvarchar,err_time nvarchar,blogapp nvarchar)");
		db.execSQL("CREATE TABLE IF NOT EXISTS "
				+ AppConfig.DB_BLOG_DRAFT_TB
				+ "(id INTEGER PRIMARY KEY AUTOINCREMENT ,txbTitle nvarchar,EditorBody nvarchar,name_site_categroy_id INTEGER,cbHomeCandidate INTEGER,cbIsPublishToSiteHome INTEGER,chkDisplayHomePage INTEGER,txbExcerpt nvarchar,txbTag nvarchar,err_text nvarchar,draft_time nvarchar)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		try {
			db.execSQL("DROP TABLE IF EXISTS " + AppConfig.DB_BLOG_LINE_TB);
			db.execSQL("DROP TABLE IF EXISTS " + AppConfig.DB_NEW_LINE_TB);
			db.execSQL("DROP TABLE IF EXISTS " + AppConfig.DB_CONTENT_TB);
			db.execSQL("DROP TABLE IF EXISTS " + AppConfig.DB_DownLoad_TB);
			db.execSQL("DROP TABLE IF EXISTS " + AppConfig.DB_FAVORITE_TB);
			db.execSQL("DROP TABLE IF EXISTS " + AppConfig.DB_COMMENT_QUEUE_TB);
			db.execSQL("DROP TABLE IF EXISTS " + AppConfig.DB_BLOG_DRAFT_TB);
		} catch (Exception e) {
			e.printStackTrace();
		}
		onCreate(db);
	}

}
